﻿using UnityEngine;
using System.Collections;

public class EnemyCheck : MonoBehaviour {

	Transform enemyTrans;
	bool HasCollided = false;
	public float FadeTime = 0.25f;
	float StartFade = 0f;

	// Use this for initialization
	void Start () {
		StartFade = FadeTime;
		enemyTrans = this.transform;
		//GameMaintainerSingleton.Instance.PlayBeeEnterSound ();
	}
	
	// Update is called once per frame
	void Update () {
		if (this.HasCollided) {
			FadeTime -= Time.deltaTime;
			if (FadeTime >= 0f) {
				Color col = this.gameObject.renderer.material.color;
				col.a = Mathf.Lerp(0f, 1f, FadeTime * (1f / StartFade));
				this.gameObject.renderer.material.color = col;
				this.gameObject.transform.localScale *= 1.1f;
				
			}
			else {
				GameObject.Destroy(this.enemyTrans.gameObject);
			}
		}
	}
	
	void FixedUpdate() {
	}
	
	void OnCollisionEnter2D(Collision2D other) {
		if (other.collider.tag == "Player") {
			//Debug.Log("Got to collision");
			CircleCollider2D col = (CircleCollider2D)this.gameObject.GetComponent(typeof(CircleCollider2D));
			col.enabled = false;

			if (GameMaintainerSingleton.Instance.Difficulty == 0) {
				SpriteRenderer beeRenderer = (SpriteRenderer)this.gameObject.GetComponent(typeof(SpriteRenderer));
				float midY = beeRenderer.bounds.min.y + ((beeRenderer.bounds.max.y - beeRenderer.bounds.min.y) / 2.0f);
				SpriteRenderer playerRenderer = (SpriteRenderer)other.gameObject.GetComponent(typeof(SpriteRenderer));
				Rigidbody2D playerRigid = other.gameObject.rigidbody2D;
				//Debug.Log(midY + "/" + playerRenderer.bounds.min.y + "/" + playerRenderer.bounds.max.y);
				if (playerRenderer.bounds.min.y > midY) {
					GameMaintainerSingleton.Instance.KilledEnemy();
				}
				else {
					GameMaintainerSingleton.Instance.TouchedEnemy();
				}
			}
			else {
				GameMaintainerSingleton.Instance.TouchedEnemy();
			}
			this.HasCollided = true;
		}
	}
}
